//
//  ViewController.m
//  MJRefreshCrashDemo
//
//  Created by bnqc on 2018/9/10.
//  Copyright © 2018年 Dong. All rights reserved.
//

#import "ViewController.h"
#import "MJRefresh.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *mainTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadFirstDataArray];
    [self.view addSubview:self.mainTableView];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (UITableView *)mainTableView{
    
    if (_mainTableView == nil) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20) style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        [_mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mainTableViewCell"];
        [self loadRefreshViewAction];
    }
    return _mainTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainTableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44.0;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
#warning ---  问题还原
//    return 1.0f;
#warning ---  问题改正
    return 44.0f;
}

- (void)loadRefreshViewAction{
    
    [_mainTableView.mj_footer resetNoMoreData];
    MJRefreshNormalHeader *refreshHeaderView = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadFirstDataArray)];
    [refreshHeaderView setTitle:@"刷新完成" forState:MJRefreshStateNoMoreData];
    [refreshHeaderView setTitle:@"松开即刻刷新" forState:MJRefreshStatePulling];
    [refreshHeaderView setTitle:@"刷新中 ..." forState:MJRefreshStateRefreshing];
    _mainTableView.mj_header = refreshHeaderView;
    MJRefreshAutoNormalFooter *refreshFooterView = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadLastDataAction)];
    _mainTableView.mj_footer = refreshFooterView;

}

- (void)loadFirstDataArray{
    
    self.dataArray = [NSMutableArray arrayWithCapacity:10];
    
    for (int i = 1; i < 10; i++) {
        
        [self.dataArray addObject:[NSString stringWithFormat:@"测试第%d行",i]];
    }
    [self.mainTableView reloadData];
    [self.mainTableView.mj_header endRefreshing];
}

- (void)loadLastDataAction{
    
    int count = self.dataArray.count;
    
    for (int i = count; i < count + 10; i++) {
        
        [self.dataArray addObject:[NSString stringWithFormat:@"测试第%d行",i]];
    }
    [self.mainTableView reloadData];
    [self.mainTableView.mj_footer endRefreshing];
}


@end
